﻿// Found the NamedIndexedDictionary source code online at: http://weblogs.asp.net/bradvincent/archive/2007/12/13/220-berutils-part-4-collections.aspx

using System;
using System.Collections.Generic;
using System.Text;

namespace ElveTouch
{
    /// <summary>
    /// using a string as a key. Can be useful in collections where you would like to use case insensitive keys
    /// </summary>
    /// <typeparam name="TValue">type of the value</typeparam>
    public class NamedIndexedDictionary<TValue> : IndexedDictionary<string, TValue>
    {
        //allow keys the be case insensitive
        private bool m_bCaseInsensitive = false;

        #region Constructors
        public NamedIndexedDictionary() { }

        public NamedIndexedDictionary(bool bCaseInsensitive)
        {
            m_bCaseInsensitive = bCaseInsensitive;
        }

        public NamedIndexedDictionary(bool bCaseInsensitive, bool bReplaceDuplicateKeys)
            : this(bCaseInsensitive)
        {
            m_bReplaceDuplicateKeys = bReplaceDuplicateKeys;
        }

        public NamedIndexedDictionary(bool bCaseInsensitive, bool bReplaceDuplicateKeys, bool bThrowErrorOnInvalidRemove)
            : this(bCaseInsensitive, bReplaceDuplicateKeys)
        {
            m_bThrowErrorOnInvalidRemove = bThrowErrorOnInvalidRemove;
        }
        #endregion

        public bool CaseInsensitive
        {
            get { return m_bCaseInsensitive; }
        }

        protected override string TransformKey(string key)
        {
            if (m_bCaseInsensitive) return key.ToUpper(System.Globalization.CultureInfo.InvariantCulture); //key.ToUpperInvariant(); ToUpperInvariant() isn't in the compact framework.
            return key;
        }
    }
}
